<template>
  <div>
    <el-card class="box-card">
      <span><i class="iconfont icon-dingdan1 icon"> </i>数据列表</span>
      <el-button
        style="float: right; padding: 3px"
        type="primary"
        @click="goCateAddPage"
        >添加</el-button
      >

      <el-button
        style="float: right; padding: 3px; margin-right: 5px"
        type="primary "
        @click="goBackLevel"
        >返回上级节点</el-button
      >
    </el-card>
    <el-table
      class="list"
      :data="cateList"
      border
      style="width: 100%"
      align="center"
    >
      <el-table-column prop="id" label="编号" width="100"> </el-table-column>
      <el-table-column label="分类名称" prop="name"> </el-table-column>
      <el-table-column label="级别">
        <template slot-scope="scope">
          {{ scope.row.level }}
        </template>
      </el-table-column>
      <el-table-column label="商品数量" prop="product_count"> </el-table-column>
      <el-table-column label="数量单位" prop="product_unit"> </el-table-column>
      <el-table-column label="是否显示" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.show_status"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column label="排序" prop="sort"> </el-table-column>
      <el-table-column label="设置" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="showNextLevel(scope.row.id)"
            :disabled="scope.row.level == 1"
          >
            查看下级
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template>
          <el-button type="primary" size="mini">编辑</el-button>
          <el-button type="danger" size="mini">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
   <!--  current-page 当前页码
    page-sizes   每页有多少条数据的下拉列表
    page-size    当前的列表每页有多少条数据
    total        数据的总数量
    size-change   修改每页有多少条数据
    current-change 修改当前是第几页 -->

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum"  
      :page-sizes="[1, 5, 10, 15]"
      :page-size="listQuery.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>




<script>
//引入
import { fetchProductCatePage } from "@/api/product";
export default {
  name: "productCate",
  data: function () {
    return {
      cateList: [],
      //1.分页的数据
      listQuery: {
        //key的名称和后台的接收名称是一致的，不能改
        pageNum: 1, //页码，默认是第一页
        pageSize: 5, //每页显示的数量
      },
      total: 0, //数据的总条数
      parentId: 0, //父级id号  查看下一级和返回上一级
    };
  },
  methods: {
     //5.分页：修改每页显示的条数
    handleSizeChange(val){
       console.log("修改每页显示的条数 触发了", val);
      this.listQuery.pageSize = val;
      this.listQuery.pageNum = 1;
        //重新获取数据
      this.getCateList();
    },
      //6.分页：修改当前页的页码
    handleCurrentChange(val){
        console.log("修改当前页的页码 触发了", val);
      this.listQuery.pageNum = val;
      this.getCateList();
    },


    //  //获取商品分类分页数据
    async getCateList() {
      const res = await fetchProductCatePage(this.listQuery, this.parentId);
      console.log(res);
      this.cateList = res.data;
        //3.分页：获取数据的总量
      this.total = res.total;
    },
    showNextLevel(id) {
      this.parentId = id;
      this.getCateList();
    },
    goBackLevel() {
      this.parentId = 0;
      this.getCateList();
    },
    //跳转到添加页面
    goCateAddPage() {
      this.$router.push("/pms/productCateAdd");
    },
    
  },
  filters: {
    jibeiFilter(val) {
      return val ? "二级" : "一级";
    },
  },
  mounted() {
    this.getCateList();
  },
};
</script>

<style>
</style>